package com.gec.mall.controller;

import com.gec.mall.document.EsItem;
import com.gec.mall.service.EsItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: li
 * @Date: 2023/2/15
 * @PACKAGE_NAME:com.gec.mall.controller
 * @Project_Name:mall-parent
 * @VERSION: 1.0.0
 */
@RestController
@RequestMapping("/EsItem")
public class EsItemController {

    @Autowired
    private EsItemService itemService;

    @GetMapping("/findByPrefix")
    public List<EsItem> findByPrefix(String prefix){
        System.out.println("prefix: " + prefix);
        Iterator<EsItem> iterator = itemService.findByPrefix(prefix, null).iterator();
        List<EsItem> itemList = new ArrayList<>();
        while (iterator.hasNext()){
            EsItem esItem = iterator.next();
            System.out.println(esItem);
            itemList.add(esItem);
        }
        return itemList;
    }

    @GetMapping("/findEsItemById/{id}")
    public EsItem findEsItemById(@PathVariable Long id){
        System.out.println("ID: " + id);
        return itemService.findById(id);
    }
}
